<!-- 考试 -->
<template>
  <div>
    <el-tabs type="border-card" class="demo-tabs" v-model="curtab">
      <el-tab-pane name="first">
        <template #label>
          <span class="custom-tabs-label">
            <!-- <el-icon><calendar /></el-icon> -->
            <span class="circle" :class="{ currentcircle: curtab == 'first' }"></span>
            <span>全部</span>
          </span>
        </template>

        <!-- 状态描述 -->
        <div class="describe behindbox">
          <i>&#xe72d;</i>
          <span style="margin-right: 50px">已完成</span>
          <i>&#xe636;</i>
          <span>未完成</span>
          <span style="margin-left: 260px">考试时间</span>
          <span style="margin-left: 250px">时长</span>
          <span style="margin-left: 330px">状态</span>
        </div>

        <!-- 考试列表 -->
        <ul class="ulbox">
          <li @click="openExamSum(item.id)" v-for="(item, index) in exammylist" :key="index">
            <div class="iconcontainer behindbox">
              <i v-show="item.user_status != 0">&#xe72d;</i>
              <i v-show="item.user_status == 0">&#xe636;</i>
            </div>
            <p>{{ item.title }}</p>
            <p class="font14">{{ item.begin_time }}~{{ item.end_time }}</p>
            <p class="font14">120分钟</p>
            <span class="statuslabel">{{ examstatus[item.status] }}</span>
          </li>
        </ul>
      </el-tab-pane>
      <el-tab-pane label="未开始" name="second">
        <template #label>
          <span class="custom-tabs-label">
            <span class="circle" :class="{ currentcircle: curtab == 'second' }"></span>
            <span>未开始</span>
          </span>
        </template>

        <!-- 状态描述 -->
        <div class="describe behindbox">
          <i>&#xe72d;</i>
          <span style="margin-right: 50px">已完成</span>
          <i>&#xe636;</i>
          <span>未完成</span>
          <span style="margin-left: 260px">考试时间</span>
          <span style="margin-left: 250px">时长</span>
          <span style="margin-left: 330px">状态</span>
        </div>

        <!-- 考试列表 -->
        <ul class="ulbox">
          <li @click="openExamSum(item.id)" v-for="(item, index) in nostartlist" :key="index">
            <div class="iconcontainer behindbox">
              <i v-show="item.user_status != 0">&#xe72d;</i>
              <i v-show="item.user_status == 0">&#xe636;</i>
            </div>
            <p>{{ item.title }}</p>
            <p class="font14">{{ item.begin_time }}~{{ item.end_time }}</p>
            <p class="font14">120分钟</p>
            <span class="statuslabel">{{ examstatus[item.status] }}</span>
          </li>
        </ul>
      </el-tab-pane>
      <el-tab-pane label="进行中" name="third">
        <template #label>
          <span class="custom-tabs-label">
            <!-- <el-icon><calendar /></el-icon> -->
            <span class="circle" :class="{ currentcircle: curtab == 'third' }"></span>
            <span>进行中</span>
          </span>
        </template>

        <!-- 状态描述 -->
        <div class="describe behindbox">
          <i>&#xe72d;</i>
          <span style="margin-right: 50px">已完成</span>
          <i>&#xe636;</i>
          <span>未完成</span>
          <span style="margin-left: 260px">考试时间</span>
          <span style="margin-left: 250px">时长</span>
          <span style="margin-left: 330px">状态</span>
        </div>

        <ul class="ulbox">
          <li @click="openExamSum(item.id)" v-for="(item, index) in currentlist" :key="index">
            <div class="iconcontainer behindbox">
              <i v-show="item.user_status != 0">&#xe72d;</i>
              <i v-show="item.user_status == 0">&#xe636;</i>
            </div>
            <p>{{ item.title }}</p>
            <p class="font14">{{ item.begin_time }}~{{ item.end_time }}</p>
            <p class="font14">120分钟</p>
            <span class="statuslabel">{{ examstatus[item.status] }}</span>
          </li>
        </ul>
      </el-tab-pane>

      <el-tab-pane label="已结束" name="fourth">
        <template #label>
          <span class="custom-tabs-label">
            <!-- <el-icon><calendar /></el-icon> -->
            <span class="circle" :class="{ currentcircle: curtab == 'fourth' }"></span>
            <span>已结束</span>
          </span>
        </template>

        <!-- 状态描述 -->
        <div class="describe behindbox">
          <i>&#xe72d;</i>
          <span style="margin-right: 50px">已完成</span>
          <i>&#xe636;</i>
          <span>未完成</span>
          <span style="margin-left: 260px">考试时间</span>
          <span style="margin-left: 250px">时长</span>
          <span style="margin-left: 330px">状态</span>
        </div>

        <ul class="ulbox">
          <li @click="openExamSum(item.id)" v-for="(item, index) in overlist" :key="index">
            <div class="iconcontainer behindbox">
              <i v-show="item.user_status != 0">&#xe72d;</i>
              <i v-show="item.user_status == 0">&#xe636;</i>
            </div>
            <p>{{ item.title }}</p>
            <p class="font14">{{ item.begin_time }}~{{ item.end_time }}</p>
            <p class="font14">120分钟</p>
            <span class="statuslabel">{{ examstatus[item.status] }}</span>
          </li>
        </ul>
      </el-tab-pane>
    </el-tabs>

    <el-dialog v-model="dialogExamVisible" width="800">
      <div class="studentinfo">
        <div class="name_class">
          <ul>
            <li>
              <div class="labelbox">姓名：</div>
              <div class="contentbox">luffygo</div>
            </li>
            <li>
              <div class="labelbox">班级：</div>
              <div class="contentbox">{{ examdetail.class_name }}</div>
            </li>
            <li>
              <div class="labelbox">课程：</div>
              <div class="contentbox">{{ examdetail.course_name }}</div>
            </li>
          </ul>
        </div>
        <div class="score">
          <p>成绩：</p>
          <!-- <p>成绩评定中</p> -->
          <p>80分</p>

        </div>
      </div>
      <div class="examinfo">
        <div>
          <ul>
            <li>
              <div class="labelbox">考试名称：</div>
              <div class="contentbox">{{ examdetail.title }}</div>
            </li>
            <li>
              <div class="labelbox">考试时间：</div>
              <div class="contentbox">{{ examdetail.begin_time }}~{{ examdetail.end_time }}</div>
            </li>
            <li>
              <div class="labelbox">考试限时：</div>
              <div class="contentbox">{{ examdetail.duration / 60 }}分钟</div>
            </li>
            <li>
              <div class="labelbox">考试总分：</div>
              <div class="contentbox">{{ examdetail.score_total }}</div>
            </li>
            <li>
              <div class="labelbox">合格分数：</div>
              <div class="contentbox">{{ examdetail.score }}</div>
            </li>
            <li>
              <div class="labelbox">题目数量：</div>
              <div class="contentbox">{{ examdetail.qnum }}</div>
            </li>
            <li>
              <div class="labelbox">题目类型：</div>
              <div class="contentbox">{{ examdetail.qtype }}</div>
            </li>
            <li>
              <div class="labelbox">考试状态：</div>
              <div class="contentbox">{{ examstatus[examdetail.status] }}</div>
            </li>
            <li>
              <div class="labelbox">完成状态：</div>
              <div class="contentbox">{{ userstatus[examdetail.user_status] }}</div>
            </li>
          </ul>
        </div>
        <div class="btnbox">
          <el-button type="primary" class="btnstyle" @click="gotoExamDetail">查询试卷详情</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, inject, defineProps } from 'vue'
import { useRouter } from 'vue-router'

// 加载user模块 以及底下变量
import { storeToRefs } from 'pinia'
import useStore from '@/store'
const { user } = useStore()
let { username, avatar, uid } = storeToRefs(user)

// 定义props
const props = defineProps({
  lessonid: {
    default: 0,
    required: true,
    type: Number
  }
})

let exammylist = ref([])  // 全部
let overlist = ref([])    // 已结束
let currentlist = ref([]) // 进行中
let nostartlist = ref([]) // 未开始

let examstatus = ref(['未开始', '进行中', '已结束'])
let userstatus = ref(['未完成', '已完成'])
let examId = ref(0)
let examdetail = ref({})

// 初始化路由实例
const router = useRouter()
// 注入网络请求方法
const axios = inject('axios')
import api from '@/request/api'

// 模态框显示标识
const dialogExamVisible = ref(false)
let curtab = ref('first')


onMounted(() => {
  console.log('props.lessonid', props.lessonid)
  getmyExamlist()
})

// 获取课程
async function getmyExamlist() {
  let res = await axios.post(api.exammylist, {
    uid: uid.value,
    course_id: 121, // 按课程筛选
    // page: 1,
    // limit: 20
  })

  if (res.code == 0) {
    console.log('exammylist', res.data)
    exammylist.value = res.data

    // 分类
    let nostart = []
    let current = []
    let over = []
    res.data.forEach(item => {
      switch (item.status) {
        case 0:
          nostart.push(item) // 未开始
          break;
        case 1:
          current.push(item) // 进行中
          break;
        case 2:
          over.push(item) // 已结束
          break;
        default:
          break;
      }
    });

    // 赋值
    nostartlist.value = nostart
    currentlist.value = current
    overlist.value = over

  } else {
    showMessage('error', res.msg)
  }
}

// 打开考试概述
async function openExamSum(eid) {
  examId.value = eid
  console.log(eid);
  // 请求考试详情
  let res = await axios.post(api.examcoursedetail, { uid: uid.value, id: eid })
  console.log(res);

  if (res.code == 0) {
    examdetail.value = res.data

    // 统计题目数量和类型
    let temobj = await getExamQuestions(eid)
    examdetail.value.qnum = temobj.qnum
    examdetail.value.qtype = temobj.qtype

    dialogExamVisible.value = true
  } else {
    showMessage('error', '考卷详情加载失败：' + res.msg)
  }
}

// 跳转考试详情
function gotoExamDetail() {
  // 新页面打开
  const hrefstr = router.resolve({
    name: 'Examdetail',
    query: {
      eid: examId.value
    }
  }).href

  // 隐藏弹出框
  dialogExamVisible.value = false

  let timer = setTimeout(() => {
    clearTimeout(timer)
    window.open(hrefstr, '_blank')
  }, 300);
}

// 获取考题列表
async function getExamQuestions(pid) {

  let res = await axios.post(api.exambindlist, { pid: pid })
  console.log(res);

  let single = []   // 单选
  let multiple = [] // 多选
  let fillin = []   // 填空
  let longfill = [] // 简答
  let judge = [] // 判断

  if (res.code == 0) {
    // console.log('exammylist', res.data)
    res.data.forEach(item => {
      switch (item.ctype) {
        // 单选
        case 1:
          single.push(item)
          break;
        // 多选
        case 2:
          multiple.push(item)
          break;

        // 填空
        case 4:
          fillin.push(item)
          break;
        // 判断
        case 5:
          judge.push(item)
          break;
        // 简答
        case 6:
          longfill.push(item)
          break;
        default:
          break;
      }
    });

    let qtype = ''
    let qnum = 0

    if (single.length != 0) {
      qtype += `${single.length}选择题；`
      qnum += single.length
    }
    if (multiple.length != 0) {
      qtype += `${multiple.length}多选题；`
      qnum += multiple.length
    }
    if (judge.length != 0) {
      qtype += `${judge.length}判断题；`
      qnum += judge.length
    }
    if (fillin.length != 0) {
      qtype += `${fillin.length}填空题；`
      qnum += fillin.length
    }
    if (longfill.length != 0) {
      qtype += `${longfill.length}简答题`
      qnum += longfill.length
    }

    return { qtype, qnum }
  } else {
    showMessage('error', res.msg)
    return {}
  }
}

// 信息提示
const showMessage = (msgtype, msgstr) => {
  ElMessage({
    showClose: true,
    message: msgstr,
    type: msgtype
  })
}
</script>

<style scoped>
.demo-tabs>.el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

.demo-tabs .custom-tabs-label .el-icon {
  vertical-align: middle;
}

.demo-tabs .custom-tabs-label span {
  vertical-align: middle;
  margin-left: 4px;
}

.circle {
  box-sizing: border-box;
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 4px;
  /* background-color: skyblue; */
  border: 1px solid skyblue;
}

.describe {
  display: flex;
  align-items: center;
  height: 30px;
  margin-bottom: 20px;
}

.describe span {
  font-weight: 700 !important;
}

.currentcircle {
  background-color: skyblue;
  border: 0;
}

.behindbox i {
  display: block;
  font-family: 'iconfont2' !important;
  font-size: 16px;
  color: #afc5cc;
  margin-right: 10px;
}

.ulbox li {
  display: flex;
  align-items: center;
  height: 40px;
  /* background-color: pink; */
  border-bottom: 1px solid #eee;
  cursor: pointer;
}

.ulbox li p {
  width: 370px;
  /* margin-right: 90px; */
}

.ulbox li .font14 {
  font-size: 14px;
}

.statuslabel {
  font-size: 14px;
}

.studentinfo {
  display: flex;
  width: 90%;
  height: 150px;
  margin: 0 auto;
  /* background-color: pink; */
  margin-bottom: -50px;
  padding-top: 20px;
}

.examinfo {
  width: 90%;
  height: 400px;
  margin: 0 auto;
  /* background-color: skyblue; */
  padding-top: 30px;
}

.studentinfo ul li,
.examinfo ul li {
  display: flex;
  height: 28px;
  margin: 0px auto;
  align-items: center;
  font-size: 16px;
}

li .labelbox {
  width: 80px;
  margin-left: 20px;
  margin-right: 10px;
  /* background-color: yellowgreen; */
}

li .contentbox {
  width: 360px;
  margin-right: 10px;
  /* background-color: orange; */
}

.examinfo ul li div {
  font-weight: 700 !important;
}

.btnbox {
  text-align: center;
  margin-top: 40px;
}

.btnstyle {
  width: 200px;
  margin: 0 auto;
}

.studentinfo .score {
  display: flex;
  width: 240px;
  height: 100%;
  margin-left: -60px;
  /* align-items: center; */
}

.studentinfo .score p {
  color: #0099fe;
  font-size: 30px;
  font-weight: 700;
}
</style>